package com.usian.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.usian.admin.mapper.AdSensitiveMapper;
import com.usian.admin.service.AdSensitiveService;
import com.usian.model.admin.dtos.SensitiveDto;
import com.usian.model.admin.pojos.AdSensitive;
import com.usian.model.common.dtos.PageResponseResult;
import com.usian.model.common.dtos.ResponseResult;
import com.usian.model.common.enums.AppHttpCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;


@Service
public class AdSensitiveServiceImpl extends ServiceImpl<AdSensitiveMapper, AdSensitive> implements  AdSensitiveService{

    @Autowired
    private AdSensitiveMapper adSensitiveMapper;

    @Override
    public ResponseResult findByNameAndPage(SensitiveDto dto) {
       //判断dto是否存在
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //检查分页参数
        dto.checkParam();
        //分页条件编写 实现分页
        LambdaQueryWrapper<AdSensitive> wrapper=new LambdaQueryWrapper<>();
        wrapper.like(dto.getName()!=null, AdSensitive::getSensitives,dto.getName());
        Page page = new Page(dto.getPage(), dto.getSize());
        IPage page1 = page(page, wrapper);
        //数据封装
        PageResponseResult result = new PageResponseResult(dto.getPage(),dto.getSize(),(int)page.getTotal());
        result.setData(page1.getRecords());
        return result;
    }

    @Override
    public ResponseResult insert(AdSensitive dto) {
        //判断dto是否存在
        if(dto==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //手动设置时间
        dto.setCreatedTime(new Date());
        //保存
        save(dto);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult update(AdSensitive dto) {
        //判断dto是否存在
        if(dto==null || dto.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //修改
        updateById(dto);
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public ResponseResult del(Integer id) {
        //判断id是否存在
        if(id==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID);
        }
        //删除
        removeById(id);
        //返回结果
        return ResponseResult.errorResult(AppHttpCodeEnum.SUCCESS);
    }

    @Override
    public List<AdSensitive> findAll() {
        return adSensitiveMapper.selectList(null);
    }
}
